let requestUrl = null;

function addHandler() {
	requestUrl = "/baoshi_category/insert";

	$("#baoshi_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#baoshi_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/baoshi_category/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#note").textbox("setValue", rowData.note);
		$("#propertyId").combobox("setValue", rowData.propertyId);
		$("#propertyValue").numberspinner("setValue", rowData.propertyValue);

		$("#baoshi_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#name").textbox({
		width: 150,
		required: true
	});

	$("#propertyId").combobox({
		url: "/property/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto"
	});

	$("#propertyValue").numberspinner({
		width: 150,
		required: true
	});

	$("#note").textbox({
		width: 380,
		height: 100,
		required: true,
		multiline: true
	});
	
	$("#baoshi_dialog").dialog({
		title: " ",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#baoshi_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#baoshi_dialog").dialog("close");
						$("#baoshi_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#baoshi_form").form("clear");
				$("#baoshi_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#baoshi_list").datagrid({
		url: "/baoshi_category/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}],
		columns: [[
			{field: "id", title: "编号", align: "center", hidden: true},
			{field: "name", title: "宝石名称", align: "center", width: 40},
			{field: "image", title: "宝石图片", align:"center", width: 40
				, formatter: function(value) {
					return "<img height='" + size + "' src='" + value + "' />";
				}
			},
			{field: "propertyId", title: "属性", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/property/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#property_" + rowIndex).html(data.name);
					}, error);

					return "<div id='property_" + rowIndex + "'></div>";
				}
			},
			{field: "propertyValue", title: "属性值", align: "center", width: 50},
			{field: "note", title: "说明", align: "center", width: 200
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			}
		]]
	});

});